<?php

namespace App\Models;

use Illuminate\Auth\Authenticatable;
use Illuminate\Contracts\Auth\Access\Authorizable as AuthorizableContract;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Laravel\Lumen\Auth\Authorizable;
use DateTimeInterface;

class Cms_Evaluate extends Model implements AuthenticatableContract, AuthorizableContract
{
    use Authenticatable, Authorizable, HasFactory;
    /**
     * 关联到模型的数据表
     *
     * @var string
     */
    protected $table = 'cms_evaluate';

    /**
     * The primary key associated with the table.
     *
     * @var string
     */
    protected $primaryKey = 'id';

    protected $fillable = [
        'storeid',
        'storename',
        'staffid',
        'staffname',
        'attitude',
        'quality',
        'environment',
        'feedback',
        'username',
        'phone',
        'status',
        'days',
        'thirdQ1',
        'thirdQ2',
        'thirdQ3',
        'fourteenQ1',
        'fourteenQ2',
        'fourteenQ3'
    ];

    protected function serializeDate(DateTimeInterface $date)
    {
        return $date->format('Y-m-d H:i:s');
    }

    public static function getFilter($filter)
    {
        return self::where(function ($query) use ($filter){
            if (isset($filter['storeid']) && !empty($filter['storeid'])) {
                $query->where('storeid', $filter['storeid']);
            }

            if (isset($filter['staffid']) && !empty($filter['staffid'])) {
                $query->where('staffid', $filter['staffid']);
            }

            if (isset($filter['storename']) && !empty($filter['storename'])) {
                $query->where('storename', 'LIKE', '%' . $filter['storename'] . '%');
            }

            if (isset($filter['staffname']) && !empty($filter['staffname'])) {
                $query->where('staffname', 'LIKE', '%' . $filter['staffname'] . '%');
            }
        });
    }
}
